package com.pn.controller;

import com.pn.dto.AssignRoleDto;
import com.pn.entity.*;
import com.pn.mapper.UserMapper;
import com.pn.page.Page;
import com.pn.service.RoleService;
import com.pn.service.UserRoleService;
import com.pn.service.UserService;
import com.pn.utils.TokenUtils;
import com.pn.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/user-list")
    public Result userList(Page page, User user){
        System.out.println("page = " + page + ", user = " + user);
        Page res = userService.queryUserByPage(page, user);

        return Result.ok(res);
    }

    @Autowired
    private TokenUtils tokenUtils;

    @RequestMapping("/addUser")
    public Result addUser(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){

        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int createBy = currentUser.getUserId();
        user.setCreateBy(createBy);
        Result result = userService.saveUser(user);
        return result;
    }

    @RequestMapping("/updateState")
    public Result updateUserState(@RequestBody User user){
        Result result = userService.setUserState(user);

        return result;
    }
    @Autowired
    private UserRoleService userRoleService;

    @RequestMapping("/user-role-list/{userId}")
    public Result userRoleList(@PathVariable Integer userId){
        List<Role> userRoles = userRoleService.queryUserRole(userId);
        return Result.ok(userRoles);
    }
    @Autowired
    private RoleService roleService;

    @RequestMapping("/assignRole")
    public Result assignRole(@RequestBody AssignRoleDto assignRoleDto){

        //执行业务
        roleService.assignRole(assignRoleDto);
        //响应
        return Result.ok("分配角色成功！");
    }

    /**
     * 删除用户的url接口/user/deleteUser/{userId}
     */
    @RequestMapping("/deleteUser/{userId}")
    public Result deleteUser(@PathVariable Integer userId){
        //执行业务
        userService.deleteUserById(userId);
        //响应
        return Result.ok("用户删除成功！");
    }

    /**
     * 修改用户的url接口/user/updateUser
     *
     * @RequestBody User user将请求传递的json数据封装到参数User对象;
     * @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token
     * 将请求头Token的值即客户端归还的token赋值给参数变量token;
     */
    @RequestMapping("/updateUser")
    public Result updateUser(@RequestBody User user,
                             @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //获取当前登录的用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录的用户id -- 修改用户的用户id
        int updateBy = currentUser.getUserId();

        user.setUpdateBy(updateBy);

        //执行业务
        Result result = userService.updateUserName(user);

        //响应
        return result;
    }

    /**
     * 重置密码的url接口/user/updatePwd/{userId}
     */
    @RequestMapping("/updatePwd/{userId}")
    public Result resetPassWord(@PathVariable Integer userId){
        //执行业务
        Result result = userService.resetPwd(userId);
        //响应
        return result;
    }
}
